package com.midi.wholesale.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.awt.Component;
import javax.swing.JComboBox;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.ImageIcon;

import com.midi.wholesale.main.Main;
import com.midi.wholesale.main.PasswordChecker;
import com.midi.wholesale.main.addElements;
import com.midi.wholesale.ui.DatePicker;
import javax.swing.JTable;
import javax.swing.plaf.FontUIResource;
import javax.swing.table.DefaultTableModel;

import com.midi.wholesale.main.*;
import com.midi.wholesale.models.Agent;
import com.midi.wholesale.models.Item;
import com.midi.wholesale.models.Lorry;
import com.midi.wholesale.models.Route;
import javax.swing.JList;

public class Whole_Sale_Main_Ui extends JFrame {

	private JPanel contentPane;

	public static final JTabbedPane tabbedPane = new JTabbedPane(
			JTabbedPane.TOP);
	private JTextField ld_date_text;
	private JTable tableItems;
	private JTable de_tableItems;
	private JTextField ci_agentName_textField;
	ListSelectionListener listSelectionListenerAgency;
	ListSelectionListener listSelectionListenerlorry;
	ListSelectionListener listSelectionListenerroute;
	ListSelectionListener listSelectionListeneritem;

	static String ValAgency;
	static String Vallorry;
	static String ValRoute;
	static String ValItem;

	private JComboBox ld_route_combo = new JComboBox();
	private JComboBox ld_agent_combo = new JComboBox();

	private ArrayList lorries;
	private ArrayList routes;
	private ArrayList agents;
	private ArrayList items = new ArrayList();

	private JList agents_list;
	private JList lorry_list;
	private JList route_list;
	private JList item_list;
	private JTextField sd_amount_text;

	private int addElementTabClickCount;

	ListSelectionListener listSelectionListenerItem;

	DefaultListModel modelAgency = new DefaultListModel();
	DefaultListModel modelLorry = new DefaultListModel();
	DefaultListModel modelRoute = new DefaultListModel();
	DefaultListModel modelItem = new DefaultListModel();

	private JTextField ci_lorryName_textField;
	private JTextField ci_routeName_textField;
	private JTextField ci_itemName_textField;

	private static JTextField textFromDate;
	private static JTextField textToDate;

	Lorry selectedLorry;
	Lorry selectedLorryend;

	//protected JComboBox comboBox_rep_agent;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Whole_Sale_Main_Ui frame = new Whole_Sale_Main_Ui();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private boolean saveLorryItemInfo(Lorry lorry, Agent agent, Route route) {

		String itemName = "", amount = "";
		for (int i = 0; i < tableItems.getRowCount(); i++) {
			for (int j = 0; j < tableItems.getColumnCount(); j++) {

				if (j == 0)
					itemName = tableItems.getValueAt(i, j).toString();
				if (j == 1) {
					if (tableItems.getValueAt(i, j) == null)
						continue;
					amount = tableItems.getValueAt(i, j).toString();
					System.out.println(itemName + amount);
					Common.editDelivery(selectedLorry.getName(), itemName,
							amount);

				}

			}
		}

		try {
			/*
			ResultSet amountRes = Main.dbConnection.createStatement()
					.executeQuery(
							"select amount from items where id='"+item_id+"'");
			
			
			if(Integer.parseInt(amount.toString())>Integer.parseInt(amountRes.getObject(0).toString()))
					{
				JOptionPane.showMessageDialog(null, "No sufficient items in stock");
				
					}*/


			
			Main.dbConnection.createStatement().executeUpdate(
					"INSERT INTO lorries (id, name, agent_id,route_id) VALUES ('"
							+ lorry.getId() + "', '" + lorry.getName() + "','"
							+ agent.getId() + "','" + route.getId() + "') "
							+ "ON DUPLICATE KEY UPDATE id='" + lorry.getId()
							+ "', name='" + lorry.getName() + "', agent_id='"
							+ agent.getId() + "',route_id='" + route.getId()
							+ "'");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;

	}

	// method to save data into delivery info table

	private String getDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String time = sdf.format(date);
		return time;
	}

	private boolean saveDelivaryInfo(String lorry_id, String agent_id,
			String route_id) {

		String amount = "", remainder = "", item_id = "";
		for (int i = 0; i < de_tableItems.getRowCount(); i++) {
			for (int j = 0; j < de_tableItems.getColumnCount(); j++) {

				try {
					if (j == 0) {
						ResultSet res1 = Main.dbConnection.createStatement()
								.executeQuery(
										"select id from items where name='"
												+ de_tableItems
														.getValueAt(i, j)
														.toString() + "'");
						res1.next();
						item_id = res1.getString("id");
					}

					if (j == 1)
						amount = de_tableItems.getValueAt(i, j).toString();
					if (j == 2) {
						if (de_tableItems.getValueAt(i, j) == null)
							continue;
						remainder = de_tableItems.getValueAt(i, j).toString();
						System.out.println(amount + remainder);

						Main.dbConnection
								.createStatement()
								.executeUpdate(
										"INSERT INTO deliveryinfo (route_id, agent_id, lorry_id, item_id, amount, remainder, date) VALUES ('"
												+ route_id
												+ "', '"
												+ agent_id
												+ "','"
												+ lorry_id
												+ "', '"
												+ item_id
												+ "', '"
												+ amount
												+ "', '"
												+ remainder
												+ "', '"
												+ getDate()
												+ "') ON DUPLICATE KEY UPDATE route_id='"
												+ route_id
												+ "', agent_id='"
												+ agent_id
												+ "', lorry_id='"
												+ lorry_id
												+ "', item_id='"
												+ item_id
												+ "', amount='"
												+ amount
												+ "', remainder='"
												+ remainder
												+ "', date='"
												+ getDate() + "'");

					
						Main.dbConnection.createStatement().executeUpdate(
								"INSERT INTO delivery (lorry_id, item_id, amount) VALUES ('"
										+ lorry_id + "', '" + item_id + "','" + remainder
										+ "') ON DUPLICATE KEY UPDATE lorry_id='" + lorry_id
										+ "', item_id='" + item_id + "', amount='" + remainder
										+ "'");
						
						
						
							// at the day end calculate the sales and reduce the amount from items table
							Main.dbConnection.createStatement().executeUpdate(
									"UPDATE items AS i INNER JOIN deliveryinfo AS d ON i.id=d.item_id SET i.amount=i.amount-(d.amount-'"+remainder+"')WHERE d.lorry_id='"+ lorry_id +"'AND d.agent_id='"+agent_id+"' AND d.route_id='"+route_id+"'AND d.item_id='"+item_id+"'");
										
						
					
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
				

			}
		}
		return true;

	}

	/**
	 * Create the frame.
	 */
	public Whole_Sale_Main_Ui() {

		UIManager.put(
			    "OptionPane.messageFont",
			    new FontUIResource(new Font("Iskoola Pota", Font.PLAIN, 11))
			);
		
		setResizable(false);
		final JFrame home = this;
		setTitle("WholeSale v1.0 - Lathpandura Holdings");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 538, 694);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		// contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		tabbedPane.setBounds(10, 10, 500, 634);
		contentPane.add(tabbedPane);

		JPanel loadLorryPanel = new JPanel();
		loadLorryPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		tabbedPane.addTab("Delivery", null, loadLorryPanel, null);
		loadLorryPanel.setLayout(null);

		JLabel lblLorry = new JLabel("Lorry");
		lblLorry.setBounds(105, 24, 46, 18);
		loadLorryPanel.add(lblLorry);

		JLabel lblAgent = new JLabel("Sales Agent");
		lblAgent.setBounds(92, 343, 77, 14);
		loadLorryPanel.add(lblAgent);

		final JComboBox ld_lorry_combo = new JComboBox();
		ld_lorry_combo.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
		ld_lorry_combo.setBounds(246, 24, 168, 20);
		loadLorryPanel.add(ld_lorry_combo);

		final JComboBox ld_agent_combo = new JComboBox();
		ld_agent_combo.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
		ld_agent_combo.setBounds(233, 341, 168, 20);
		loadLorryPanel.add(ld_agent_combo);

		JLabel lblRoute = new JLabel("Route");
		lblRoute.setBounds(92, 385, 46, 14);
		loadLorryPanel.add(lblRoute);

		final JComboBox ld_route_combo = new JComboBox();
		ld_route_combo.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
		ld_route_combo.setBounds(233, 383, 168, 20);
		loadLorryPanel.add(ld_route_combo);

		ld_lorry_combo.removeAllItems();
		ld_lorry_combo.insertItemAt("Choose Lorry", 0);
		ld_lorry_combo.setSelectedIndex(0);

		ld_agent_combo.removeAllItems();
		ld_agent_combo.insertItemAt("Choose Agent", 0);
		ld_agent_combo.setSelectedIndex(0);

		ld_route_combo.removeAllItems();
		ld_route_combo.insertItemAt("Choose Route", 0);
		ld_route_combo.setSelectedIndex(0);

		final JComboBox ld_newItem_combo = new JComboBox();
		ld_newItem_combo.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
		ld_newItem_combo.insertItemAt("Choose Item", 0);
		ld_newItem_combo.setSelectedIndex(0);

		ArrayList<Agent> agentList = Common.loadagents();
		for (Agent agent : agentList) {
			ld_agent_combo.addItem(agent);
		}

		ArrayList<Lorry> lorryList = Common.loadlorries();
		for (Lorry lorry : lorryList) {
			ld_lorry_combo.addItem(lorry);
		}

		ArrayList<Route> routeList = Common.loadroutes();
		for (Route route : routeList) {
			ld_route_combo.addItem(route);
		}
		JLabel lblDate = new JLabel("Date");
		lblDate.setBounds(92, 306, 46, 14);
		loadLorryPanel.add(lblDate);

		ld_date_text = new JTextField();
		ld_date_text.setBounds(233, 303, 168, 20);
		loadLorryPanel.add(ld_date_text);
		ld_date_text.setColumns(10);

		JButton buttonCalendar = new JButton("");
		buttonCalendar.setIcon(new ImageIcon(Whole_Sale_Main_Ui.class
				.getResource("/com/midi/wholesale/resources/date.gif")));
		buttonCalendar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ld_date_text.setText(new DatePicker(home).setPickedDate());
			}
		});
		buttonCalendar.setBounds(411, 302, 21, 23);
		loadLorryPanel.add(buttonCalendar);

		JScrollPane lorry_table_scrollPane = new JScrollPane();
		lorry_table_scrollPane.setBounds(23, 67, 288, 131);
		loadLorryPanel.add(lorry_table_scrollPane);

		tableItems = new JTable() {
			public boolean isCellEditable(int row, int column) {
				return column != 0;
			}
		};
		lorry_table_scrollPane.setViewportView(tableItems);
		tableItems.setCellSelectionEnabled(true);
		tableItems.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
		tableItems.getTableHeader().setFont( new Font("Tahoma", Font.BOLD, 12));
		tableItems.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
		tableItems.setModel(new DefaultTableModel(new Object[][] {

		}, new String[] { "Item Name", "Amount" }));

		JButton lbl_btnLoadExisting = new JButton("View Existing Items");
		lbl_btnLoadExisting.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				if (!ld_lorry_combo.getSelectedItem().toString()
						.equals("Choose Lorry")) {
					ArrayList<HashMap<String, String>> items = Common
							.loadDelivery((Lorry) ld_lorry_combo
									.getSelectedItem());
					selectedLorry = (Lorry) ld_lorry_combo.getSelectedItem();
					ArrayList<String> allItemlist = new ArrayList();
					ArrayList<Item> tempallItemlist = Common.loadItems();

					
					
					for (Item it : tempallItemlist) {
						allItemlist.add(it.toString());
					}

					int i;
					Object[][] modelArray = null;
					modelArray = new Object[items.size()][2];
					for (i = 0; i < items.size(); i++) {
						modelArray[i][0] = items.get(i).get("item_name");
						modelArray[i][1] = items.get(i).get("amount");
						allItemlist.remove(items.get(i).get("item_name"));
					}

					System.out.println(allItemlist);
					
					ld_newItem_combo.removeAllItems();
					ld_newItem_combo.insertItemAt("Choose Item", 0);
					ld_newItem_combo.setSelectedIndex(0);
					
					for (String it : allItemlist) {
						ld_newItem_combo.addItem(it);
					}

					tableItems.setModel(new DefaultTableModel(modelArray,
							new String[] { "Item Name", "Amount" }));
				}
			}
		});
		lbl_btnLoadExisting.setBounds(175, 222, 151, 23);
		loadLorryPanel.add(lbl_btnLoadExisting);

		ld_newItem_combo.setBounds(359, 125, 98, 20);
		loadLorryPanel.add(ld_newItem_combo);

		JButton btnAddItem = new JButton("Add");
		btnAddItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				String newItem = "";
				if (!ld_newItem_combo.getSelectedItem().toString()
						.equals("Choose Item")) {

					newItem = ld_newItem_combo.getSelectedItem().toString();
					Object[][] rowData = { { newItem, null } };
					DefaultTableModel dm = (DefaultTableModel) tableItems
							.getModel();
					dm.addRow(rowData[0]);
					ld_newItem_combo.removeItemAt(ld_newItem_combo
							.getSelectedIndex());
				}
			}
		});
		btnAddItem.setBounds(394, 156, 63, 23);
		loadLorryPanel.add(btnAddItem);

		JLabel lblAddNewItem = new JLabel("Add New Item");
		lblAddNewItem.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblAddNewItem.setBounds(349, 90, 108, 14);
		loadLorryPanel.add(lblAddNewItem);

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_3.setBounds(331, 71, 154, 127);
		loadLorryPanel.add(panel_3);

		JButton delivery_info_button = new JButton("Save Delivery Info");
		delivery_info_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if (!ld_lorry_combo.getSelectedItem().toString()
						.equals("Choose Lorry")
						&& !ld_agent_combo.getSelectedItem().toString()
								.equals("Choose Agent")
						&& !ld_route_combo.getSelectedItem().toString()
								.equals("Choose Route")
						&& !ld_date_text.getText().toString().equals("")) {
					Lorry lorry;
					Agent agent;
					Route route;
					lorry = (Lorry) ld_lorry_combo.getSelectedItem();
					agent = (Agent) ld_agent_combo.getSelectedItem();
					route = (Route) ld_route_combo.getSelectedItem();
					
					
					boolean saveDeliveryInfo=saveLorryItemInfo(lorry, agent, route);
					if(saveDeliveryInfo)
					{
						JOptionPane.showMessageDialog(null,
								"Delivery Information Saved Successfully");
					}
					

				} else {
					JOptionPane.showMessageDialog(null,
							"Please Enter all values Before Save");
				}

			}
		});
		delivery_info_button.setBounds(305, 447, 151, 23);
		loadLorryPanel.add(delivery_info_button);

		listSelectionListenerAgency = new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent listSelectionEvent) {
				boolean adjust = listSelectionEvent.getValueIsAdjusting();
				if (!adjust) {
					JList list = (JList) listSelectionEvent.getSource();
					int selections[] = list.getSelectedIndices();
					Object selectionValues[] = list.getSelectedValues();
					for (int i = 0, n = selections.length; i < n; i++) {
						if (i == 0) {
						}
						ValAgency = (String) selectionValues[i];
						System.out.println(" Selections valAgency: "+ ValAgency);
					}
				}
			}
		};

		listSelectionListenerlorry = new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent listSelectionEvent) {
				boolean adjust = listSelectionEvent.getValueIsAdjusting();

				if (!adjust) {
					JList list = (JList) listSelectionEvent.getSource();
					int selections[] = list.getSelectedIndices();
					Object selectionValues[] = list.getSelectedValues();
					for (int i = 0, n = selections.length; i < n; i++) {
						if (i == 0) {
						}
						Vallorry = (String) selectionValues[i];
						System.out.println(" Selections vallorry: " + Vallorry);
					}
				}
			}
		};

		listSelectionListenerroute = new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent listSelectionEvent) {
				boolean adjust = listSelectionEvent.getValueIsAdjusting();

				if (!adjust) {
					JList list = (JList) listSelectionEvent.getSource();
					int selections[] = list.getSelectedIndices();
					Object selectionValues[] = list.getSelectedValues();
					for (int i = 0, n = selections.length; i < n; i++) {
						if (i == 0) {
						}
						ValRoute = (String) selectionValues[i];
						System.out.println(" Selections valroute: " + ValRoute);
					}
				}
			}
		};

		// Day End panel
		JPanel dayEndPanel = new JPanel();
		tabbedPane.addTab("Day End", null, dayEndPanel, null);
		dayEndPanel.setLayout(null);

		JLabel de_lorry_text = new JLabel("Lorry");
		de_lorry_text.setBounds(138, 39, 46, 14);
		dayEndPanel.add(de_lorry_text);

		final JComboBox de_lorry_comboBox = new JComboBox();
		de_lorry_comboBox.setBounds(233, 36, 129, 20);
		dayEndPanel.add(de_lorry_comboBox);

		JScrollPane de_lorry_table_scrollPane = new JScrollPane();
		de_lorry_table_scrollPane.setBounds(88, 83, 315, 178);
		dayEndPanel.add(de_lorry_table_scrollPane);

		de_tableItems = new JTable() {
			public boolean isCellEditable(int row, int column) {

				if (column == 0 || column == 1) {
					return false;
				}
				return true;
			}
		};
		de_lorry_table_scrollPane.setViewportView(de_tableItems);
		de_tableItems.setCellSelectionEnabled(true);
		de_tableItems.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
		//de_tableItems.getTableHeader().setFont( new Font("Iskoola Pota", Font.PLAIN, 11));
		de_tableItems.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
		de_tableItems.setModel(new DefaultTableModel(new Object[][] {

		}, new String[] { "Item Name", "Amount", "Remainder" }));

		JButton de_view_btn = new JButton("View Existing Items");
		de_view_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				if (!de_lorry_comboBox.getSelectedItem().toString()
						.equals("Choose Lorry")) {
					ArrayList<HashMap<String, String>> items = Common
							.dayEndloadDelivery((Lorry) de_lorry_comboBox
									.getSelectedItem());
					selectedLorryend = (Lorry) de_lorry_comboBox
							.getSelectedItem();

					int i;
					Object[][] modelArray = null;
					modelArray = new Object[items.size()][2];
					for (i = 0; i < items.size(); i++) {
						modelArray[i][0] = items.get(i).get("item_name");
						modelArray[i][1] = items.get(i).get("amount");
					}
					de_tableItems
							.setModel(new DefaultTableModel(modelArray,
									new String[] { "Item Name", "Amount",
											"Remainder" }));
				}
			}
		});
		de_view_btn.setBounds(88, 287, 151, 23);
		dayEndPanel.add(de_view_btn);

		JButton btnNewButton = new JButton("Save Delivery Info");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				for (int i = 0; i < de_tableItems.getRowCount(); i++) {
					for (int j = 0; j < de_tableItems.getColumnCount(); j++) {
						if (de_tableItems.getValueAt(i, j) == null) {
							JOptionPane.showMessageDialog(null,
									"Fill all the cells in Remainder column");
							return;
						}
					}
				}

				if (!de_lorry_comboBox.getSelectedItem().toString()
						.equals("Choose Lorry")) {

					Lorry lorry = (Lorry) de_lorry_comboBox.getSelectedItem();
					try {
						ResultSet res1 = Main.dbConnection.createStatement()
								.executeQuery(
										"select id,agent_id,route_id from lorries where name='"
												+ lorry.getName() + "'");
						res1.next();
						String id, agent, route;
						id = res1.getString("id");
						agent = res1.getString("agent_id");
						route = res1.getString("route_id");
					boolean	chkSaved=saveDelivaryInfo(id, agent, route);
					if(chkSaved)
					{
						JOptionPane.showMessageDialog(null,
								"Data Saved Successfullly");						
					}

					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}

			}
		});
		btnNewButton.setBounds(249, 287, 154, 23);
		dayEndPanel.add(btnNewButton);
		
				JPanel addElementPanel = new JPanel();
				tabbedPane.addTab("Add Elements", null, addElementPanel, null);
				addElementPanel.setLayout(null);
				
						JPanel itemPanel = new JPanel();
						itemPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
						itemPanel.setLayout(null);
						itemPanel.setBounds(8, 11, 483, 176);
						addElementPanel.add(itemPanel);
						
								JScrollPane scrollPane = new JScrollPane();
								scrollPane.setBounds(235, 11, 238, 127);
								itemPanel.add(scrollPane);
								
										JLabel lblItemName = new JLabel("Name  :");
										lblItemName.setBounds(32, 64, 50, 14);
										itemPanel.add(lblItemName);
										
												ci_agentName_textField = new JTextField();
												ci_agentName_textField.setBounds(91, 64, 92, 14);
												ci_agentName_textField.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
												itemPanel.add(ci_agentName_textField);
												ci_agentName_textField.setColumns(10);
												
														JButton ci_agentAdd_btn = new JButton("Add");
														ci_agentAdd_btn.addActionListener(new ActionListener() {
															public void actionPerformed(ActionEvent arg0) {

																String newAgency = ci_agentName_textField.getText();

																boolean updateDB = true;
																Iterator itr = agents.iterator();

																if (!newAgency.trim().equals("")) {
																	while (itr.hasNext()) {
																		String returnVal = itr.next().toString();
																		if (newAgency.equalsIgnoreCase(returnVal)) {
																			updateDB = false;
																		}
																	}

																	if (updateDB) {

																		DefaultListModel newAgencyModel = (DefaultListModel) agents_list
																				.getModel();
																		newAgencyModel.addElement(newAgency);
																		agents_list.setModel(newAgencyModel);
																		addElements.editAgency(newAgency, "add");
																		clearFields(1);

																		int sizeOfArray = newAgencyModel.getSize();

																		agents.clear();
																		
																		for (int i = 0; i < sizeOfArray; i++) {
																			System.out.println(newAgencyModel.elementAt(i));
																			agents.add(newAgencyModel.elementAt(i));							
																		}
																	}else {

																		JOptionPane.showMessageDialog(null,"Duplicate value please Enter a different Name");
																		ci_agentName_textField.setText("");
																	}
																} else {
																	JOptionPane.showMessageDialog(null,"Please Enter a value Before Add");
																	ci_agentName_textField.setText("");
																}

															}
														});
														ci_agentAdd_btn.setBounds(91, 109, 82, 23);
														itemPanel.add(ci_agentAdd_btn);
														
																JLabel lblAddItem = new JLabel("Add Agent");
																lblAddItem.setFont(new Font("Tahoma", Font.BOLD, 16));
																lblAddItem.setBounds(22, 11, 95, 36);
																itemPanel.add(lblAddItem);
																
																		JButton ad_agentRemoveButton = new JButton("Remove");
																		ad_agentRemoveButton.addActionListener(new ActionListener() {
																			public void actionPerformed(ActionEvent arg0) {
																				
																				if (ValAgency != null) {

																					DefaultListModel newAgencyModel = (DefaultListModel) agents_list.getModel();

																					newAgencyModel.removeElement(agents_list.getSelectedValue());
																					agents_list.setModel(newAgencyModel);
																					addElements.editAgency(ValAgency, "remove");
																					
																					int sizeOfArray = newAgencyModel.getSize();
																					
																					agents.clear();
																					
																					for (int i = 0; i < sizeOfArray; i++) {
																						System.out.println(newAgencyModel.elementAt(i));
																						agents.add(newAgencyModel.elementAt(i));						
																					}
																				}

																				if (ValAgency == null)
																					JOptionPane.showMessageDialog(null,
																							"Select an Agency first");
																				else
																					JOptionPane.showMessageDialog(null, "You deleted "+ ValAgency + " Agency");
																			}
																		});
																		ad_agentRemoveButton.setBounds(384, 142, 89, 23);
																		itemPanel.add(ad_agentRemoveButton);
																		
																				agents_list = new JList(modelAgency);
																				scrollPane.setViewportView(agents_list);
																				agents_list.setFont(new Font("Iskoola Pota", Font.BOLD, 11));
																				agents_list.setBorder(new TitledBorder(null, "Agent List",
																						TitledBorder.LEADING, TitledBorder.TOP, null, null));
																				agents_list.setBackground(UIManager.getColor("Button.highlight"));
																				
																						JPanel panel = new JPanel();
																						panel.setLayout(null);
																						panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
																						panel.setBounds(8, 196, 483, 176);
																						addElementPanel.add(panel);
																						
																								JScrollPane scrollPane_1 = new JScrollPane();
																								scrollPane_1.setBounds(235, 11, 238, 127);
																								panel.add(scrollPane_1);
																								
																										lorry_list = new JList(modelLorry);
																										lorry_list.setFont(new Font("Iskoola Pota", Font.BOLD, 11));
																										lorry_list.setBorder(new TitledBorder(UIManager
																												.getBorder("TitledBorder.border"), "Lorry List",
																												TitledBorder.LEADING, TitledBorder.TOP, null, null));
																										lorry_list.setBackground(Color.WHITE);
																										scrollPane_1.setViewportView(lorry_list);
																										
																												JLabel label = new JLabel("Name  :");
																												label.setBounds(32, 64, 50, 14);
																												panel.add(label);
																												
																														ci_lorryName_textField = new JTextField();
																														ci_lorryName_textField.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
																														ci_lorryName_textField.setColumns(10);																													
																														ci_lorryName_textField.setBounds(91, 64, 92, 14);
																														panel.add(ci_lorryName_textField);
																														
																																JButton ci_lorryAdd_btn = new JButton("Add");
																																ci_lorryAdd_btn.addActionListener(new ActionListener() {
																																	public void actionPerformed(ActionEvent e) {

																																		String newlorry = ci_lorryName_textField.getText();

																																		boolean updateDB = true;
																																		Iterator itr = lorries.iterator();

																																		// check for the duplicate values in the list

																																		if (!newlorry.trim().equals("")) {
																																			while (itr.hasNext()) {
																																				String returnVal = itr.next().toString();
																																				if (newlorry.equalsIgnoreCase(returnVal)) {
																																					updateDB = false;

																																				}

																																			}

																																			if (updateDB) {

																																				DefaultListModel newlorryModel = (DefaultListModel) lorry_list
																																						.getModel();
																																				newlorryModel.addElement(newlorry);
																																				lorry_list.setModel(newlorryModel);
																																				addElements.editLorries(newlorry, "add");
																																				clearFields(2);

																																				int sizeOfArray = newlorryModel.getSize();

																																				lorries.clear();
																																				
																																				for (int i = 0; i < sizeOfArray; i++) {
																																					System.out.println(newlorryModel.elementAt(i));
																																					lorries.add(newlorryModel.elementAt(i));							
																																				}
																																			}else {
																																				JOptionPane.showMessageDialog(null,"Duplicate value please Enter a different Name");
																																				ci_lorryName_textField.setText("");

																																			}
																																		} else {
																																			JOptionPane.showMessageDialog(null,
																																					"Please Enter a value Before Add");
																																			ci_lorryName_textField.setText("");

																																		}

																																	}
																																});
																																ci_lorryAdd_btn.setBounds(91, 109, 82, 23);
																																panel.add(ci_lorryAdd_btn);
																																
																																		JLabel lblAddLorry = new JLabel("Add Lorry");
																																		lblAddLorry.setFont(new Font("Tahoma", Font.BOLD, 16));
																																		lblAddLorry.setBounds(22, 11, 95, 36);
																																		panel.add(lblAddLorry);
																																		
																																				JButton ad_lorryRemoveButton = new JButton("Remove");
																																				ad_lorryRemoveButton.addActionListener(new ActionListener() {
																																					public void actionPerformed(ActionEvent e) {

																																						// check if selection value from the list is not null
																																						if (Vallorry != null) {

																																							DefaultListModel newlorryModel = (DefaultListModel) lorry_list.getModel();

																																							newlorryModel.removeElement(lorry_list.getSelectedValue());
																																							lorry_list.setModel(newlorryModel);
																																							addElements.editLorries(Vallorry, "remove");

																																							int sizeOfArray = newlorryModel.getSize();

																																							lorries.clear();

																																							for (int i = 0; i < sizeOfArray; i++) {
																																								System.out.println(newlorryModel.elementAt(i));
																																								lorries.add(newlorryModel.elementAt(i));
																																							}
																																						}

																																						if (Vallorry == null)
																																							JOptionPane.showMessageDialog(null, "Select a lorry first");
																																						else
																																							JOptionPane.showMessageDialog(null, "You deleted "
																																									+ Vallorry + " lorry");
																																					}
																																				});
																																				ad_lorryRemoveButton.setBounds(384, 142, 89, 23);
																																				panel.add(ad_lorryRemoveButton);
																																				
																																						JPanel panel_1 = new JPanel();
																																						panel_1.setLayout(null);
																																						panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
																																						panel_1.setBounds(8, 382, 483, 176);
																																						addElementPanel.add(panel_1);
																																						
																																								JScrollPane scrollPane_2 = new JScrollPane();
																																								scrollPane_2.setBounds(235, 11, 238, 127);
																																								panel_1.add(scrollPane_2);
																																								
																																										route_list = new JList(modelRoute);
																																										route_list.setFont(new Font("Iskoola Pota", Font.BOLD, 11));
																																										route_list.setBorder(new TitledBorder(UIManager
																																												.getBorder("TitledBorder.border"), "Route List",
																																												TitledBorder.LEADING, TitledBorder.TOP, null, null));
																																										route_list.setBackground(Color.WHITE);
																																										scrollPane_2.setViewportView(route_list);
																																										
																																												JLabel label_1 = new JLabel("Name  :");
																																												label_1.setBounds(32, 64, 50, 14);
																																												panel_1.add(label_1);
																																												
																																														ci_routeName_textField = new JTextField();
																																														ci_routeName_textField.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
																																														ci_routeName_textField.setColumns(10);
																																														ci_routeName_textField.setBounds(91, 64, 92, 14);
																																														panel_1.add(ci_routeName_textField);
																																														
																																																JButton ci_routeAdd_btn = new JButton("Add");
																																																ci_routeAdd_btn.addActionListener(new ActionListener() {
																																																	public void actionPerformed(ActionEvent arg0) {

																																																		String newroute = ci_routeName_textField.getText();

																																																		boolean updateDB = true;
																																																		Iterator itr = routes.iterator();
																																																		
																																																		if (!newroute.trim().equals("")) {
																																																			while (itr.hasNext()) {
																																																				String returnVal = itr.next().toString();
																																																				if (newroute.equalsIgnoreCase(returnVal)) {
																																																					updateDB = false;
																																																				}
																																																			}

																																																			if (updateDB) {

																																																				DefaultListModel newrouteModel = (DefaultListModel) route_list
																																																						.getModel();
																																																				newrouteModel.addElement(newroute);
																																																				route_list.setModel(newrouteModel);
																																																				addElements.editroutes(newroute, "add");
																																																				clearFields(3);
																																																				int sizeOfArray = newrouteModel.getSize();

																																																				routes.clear();

																																																				for (int i = 0; i < sizeOfArray; i++) {
																																																					System.out.println(newrouteModel.elementAt(i));
																																																					routes.add(newrouteModel.elementAt(i));
																																																				}
																																																			}else {
																																																				JOptionPane.showMessageDialog(null,"Duplicate value please Enter a different Name");
																																																				ci_routeName_textField.setText("");
																																																			}
																																																		} else {
																																																			JOptionPane.showMessageDialog(null,"Please Enter a value Before Add");
																																																			ci_routeName_textField.setText("");
																																																		}
																																																	}
																																																});
																																																ci_routeAdd_btn.setBounds(91, 109, 82, 23);
																																																panel_1.add(ci_routeAdd_btn);
																																																
																																																		JLabel lblAddRoute = new JLabel("Add Route");
																																																		lblAddRoute.setFont(new Font("Tahoma", Font.BOLD, 16));
																																																		lblAddRoute.setBounds(22, 11, 95, 36);
																																																		panel_1.add(lblAddRoute);
																																																		
																																																				JButton ad_routeRemoveButton = new JButton("Remove");
																																																				ad_routeRemoveButton.addActionListener(new ActionListener() {
																																																					public void actionPerformed(ActionEvent e) {

																																																						if (ValRoute != null) {

																																																							DefaultListModel newrouteModel = (DefaultListModel) route_list
																																																									.getModel();
																																																							newrouteModel.removeElement(route_list.getSelectedValue());
																																																							route_list.setModel(newrouteModel);
																																																							addElements.editroutes(ValRoute, "remove");
																																																							int sizeOfArray = newrouteModel.getSize();
																																																							routes.clear();
																																																							for (int i = 0; i < sizeOfArray; i++) {
																																																								System.out.println(newrouteModel.elementAt(i));
																																																								routes.add(newrouteModel.elementAt(i));
																																																							}
																																																						}
																																																						if (ValRoute == null)
																																																							JOptionPane.showMessageDialog(null, "Select a route first");
																																																						else
																																																							JOptionPane.showMessageDialog(null, "You deleted "
																																																									+ ValRoute + " route");

																																																					}
																																																				});
																																																				ad_routeRemoveButton.setBounds(384, 142, 89, 23);
																																																				panel_1.add(ad_routeRemoveButton);
																																																				agents_list.addListSelectionListener(listSelectionListenerAgency);
																																																				lorry_list.addListSelectionListener(listSelectionListenerlorry);
																																																				route_list.addListSelectionListener(listSelectionListenerroute);

		// Stock Details panel
		JPanel stockDetailPanel = new JPanel();
		tabbedPane.addTab("Stock Detail", null, stockDetailPanel, null);
		stockDetailPanel.setLayout(null);

		JLabel sd_itemName_lbl = new JLabel("Item Name");
		sd_itemName_lbl.setBounds(130, 49, 73, 14);
		stockDetailPanel.add(sd_itemName_lbl);

		final JComboBox sd_itemName_comboBox = new JComboBox();
		sd_itemName_comboBox.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
		sd_itemName_comboBox.setBounds(253, 46, 133, 20);
		stockDetailPanel.add(sd_itemName_comboBox);

		JLabel sd_amount_lbl = new JLabel("Amount");
		sd_amount_lbl.setBounds(130, 93, 61, 14);
		stockDetailPanel.add(sd_amount_lbl);

		sd_amount_text = new JTextField();
		sd_amount_text.setBounds(253, 90, 133, 20);
		stockDetailPanel.add(sd_amount_text);
		sd_amount_text.setColumns(10);

		JButton sd_save_btn = new JButton("Add");
		sd_save_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object selected_item = sd_itemName_comboBox.getSelectedItem();
				String amount = sd_amount_text.getText();
				if (selected_item.equals("Choose Item")
						|| selected_item == null || amount == null
						|| amount.equals("")) {
					JOptionPane.showMessageDialog(null, "Nothing Added");

				} else {
					try {

						Item new_item = (Item) selected_item;
						int new_amount = Integer.parseInt(sd_amount_text
								.getText());
						int itemId = new_item.getId();
						boolean IsRecorded = new StockDetail().submitData(
								new_item.getId(), new_amount);
						if(IsRecorded)
						{
							JOptionPane.showMessageDialog(null, "Item Amount Saved Successfully!");
							
						}
						sd_itemName_comboBox.setSelectedIndex(0);
						sd_amount_text.setText("");

					} catch (NumberFormatException nfe) {
						JOptionPane
								.showMessageDialog(null, "Invalid Format...");
					}

				}

			}
		});
		sd_save_btn.setBounds(156, 147, 89, 23);
		stockDetailPanel.add(sd_save_btn);

		JButton sd_clear_btn = new JButton("Clear");
		sd_clear_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sd_itemName_comboBox.setSelectedIndex(0);
				sd_amount_text.setText("");
			}
		});
		sd_clear_btn.setBounds(258, 147, 89, 23);
		stockDetailPanel.add(sd_clear_btn);

		JPanel panel_2 = new JPanel();
		panel_2.setLayout(null);
		panel_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_2.setBounds(10, 242, 483, 176);
		stockDetailPanel.add(panel_2);

		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(235, 11, 238, 127);
		panel_2.add(scrollPane_3);

		item_list = new JList(modelItem);
		item_list.setFont(new Font("Iskoola Pota", Font.BOLD, 11));
		item_list.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Item List",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		item_list.setBackground(Color.WHITE);
		scrollPane_3.setViewportView(item_list);

		JLabel label_2 = new JLabel("Name  :");
		label_2.setBounds(32, 64, 50, 14);
		panel_2.add(label_2);

		ci_itemName_textField = new JTextField();
		ci_itemName_textField.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
		ci_itemName_textField.setColumns(10);
		ci_itemName_textField.setBounds(91, 64, 92, 14);
		panel_2.add(ci_itemName_textField);

		JButton ci_itemAdd_btn = new JButton("Add");
		ci_itemAdd_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String newitem = ci_itemName_textField.getText();

				boolean updateDB = true;
				Iterator itr = items.iterator();

				// check for the duplicate values in the list

				if (!newitem.trim().equals("")) {
					while (itr.hasNext()) {
						String returnVal = itr.next().toString();
						if (newitem.equalsIgnoreCase(returnVal)) {
							updateDB = false;

						}

					}

					if (updateDB) {

						DefaultListModel newitemModel = (DefaultListModel) item_list
								.getModel();
						newitemModel.addElement(newitem);
						item_list.setModel(newitemModel);
						addElements.edititems(newitem, "add");
						clearFields(4);

						sd_itemName_comboBox.removeAllItems();
						sd_itemName_comboBox.insertItemAt("Choose Item", 0);
						sd_itemName_comboBox.setSelectedIndex(0);

						int sizeOfArray = newitemModel.getSize();

						items.clear();

						for (int i = 0; i < sizeOfArray; i++) {
							System.out.println(newitemModel.elementAt(i));
							items.add(newitemModel.elementAt(i));
							sd_itemName_comboBox.addItem(newitemModel
									.elementAt(i));
						}

					}

					else {

						JOptionPane
								.showMessageDialog(null,
										"Duplicate value please Enter a different Name");
						ci_itemName_textField.setText("");

					}
				} else {
					JOptionPane.showMessageDialog(null,
							"Please Enter a value Before Add");
					ci_itemName_textField.setText("");

				}

			}
		});
		ci_itemAdd_btn.setBounds(91, 109, 82, 23);
		panel_2.add(ci_itemAdd_btn);

		JLabel lblAddItem_1 = new JLabel("Add Item Category");
		lblAddItem_1.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblAddItem_1.setBounds(22, 11, 177, 36);
		panel_2.add(lblAddItem_1);

		listSelectionListeneritem = new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent listSelectionEvent) {
				boolean adjust = listSelectionEvent.getValueIsAdjusting();
				// System.out.println(", Adjusting? " + adjust);
				if (!adjust) {
					JList list = (JList) listSelectionEvent.getSource();
					int selections[] = list.getSelectedIndices();
					Object selectionValues[] = list.getSelectedValues();
					for (int i = 0, n = selections.length; i < n; i++) {
						if (i == 0) {
						}
						ValItem = (String) selectionValues[i];
						System.out.println(" Selections valitem: " + ValItem);
					}
				}
			}
		};

		JButton ad_itemRemoveButton = new JButton("Remove");
		ad_itemRemoveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if (ValItem != null) {

					sd_itemName_comboBox.removeAllItems();
					sd_itemName_comboBox.insertItemAt("Choose Item", 0);
					sd_itemName_comboBox.setSelectedIndex(0);

					DefaultListModel newitemModel = (DefaultListModel) item_list
							.getModel();
					newitemModel.removeElement(item_list.getSelectedValue());
					item_list.setModel(newitemModel);
					addElements.edititems(ValItem, "remove");
					int sizeOfArray = newitemModel.getSize();
					items.clear();
					for (int i = 0; i < sizeOfArray; i++) {
						System.out.println(newitemModel.elementAt(i));
						items.add(newitemModel.elementAt(i));
						sd_itemName_comboBox.addItem(newitemModel.elementAt(i));
					}
				}
				if (ValItem == null)
					JOptionPane.showMessageDialog(null, "Select a item first");
				else
					JOptionPane.showMessageDialog(null, "You deleted "
							+ ValItem + " item");

			}
		});
		ad_itemRemoveButton.setBounds(384, 142, 89, 23);
		panel_2.add(ad_itemRemoveButton);
		item_list.addListSelectionListener(listSelectionListeneritem);

		// reports panel
		JPanel reportPanel = new JPanel();
		tabbedPane.addTab("Reports", null, reportPanel, null);
		reportPanel.setLayout(null);

		JLabel selectDatelbl = new JLabel("Select dates and agent");
		selectDatelbl.setFont(new Font("Tahoma", Font.BOLD, 16));
		selectDatelbl.setBounds(21, 11, 293, 30);
		reportPanel.add(selectDatelbl);

		JLabel fromDateLbl = new JLabel("From");
		fromDateLbl.setBounds(80, 65, 46, 14);
		reportPanel.add(fromDateLbl);

		textFromDate = new JTextField();
		textFromDate.setBounds(165, 62, 168, 20);
		reportPanel.add(textFromDate);
		textFromDate.setColumns(10);

		JButton buttonFromCalendar = new JButton("");
		buttonFromCalendar.setIcon(new ImageIcon(Whole_Sale_Main_Ui.class
				.getResource("/com/midi/wholesale/resources/date.gif")));
		buttonFromCalendar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textFromDate.setText(new DatePicker(home).setPickedDate());
			}
		});
		buttonFromCalendar.setBounds(343, 59, 21, 23);
		reportPanel.add(buttonFromCalendar);

		JLabel toDateLbl = new JLabel("To");
		toDateLbl.setBounds(80, 95, 46, 14);
		reportPanel.add(toDateLbl);

		textToDate = new JTextField();
		textToDate.setBounds(165, 92, 168, 20);
		reportPanel.add(textToDate);
		textToDate.setColumns(10);

		JButton buttonToCalendar = new JButton("");
		buttonToCalendar.setIcon(new ImageIcon(Whole_Sale_Main_Ui.class
				.getResource("/com/midi/wholesale/resources/date.gif")));
		buttonToCalendar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textToDate.setText(new DatePicker(home).setPickedDate());
			}
		});
		buttonToCalendar.setBounds(343, 89, 21, 23);
		reportPanel.add(buttonToCalendar);

		JLabel lblSalesAgent = new JLabel("Sales Agent");
		lblSalesAgent.setBounds(80, 131, 80, 14);
		reportPanel.add(lblSalesAgent);
		
		final JComboBox comboBox_rep_agent = new JComboBox();
		comboBox_rep_agent.setFont(new Font("Iskoola Pota", Font.PLAIN, 11));
		comboBox_rep_agent.setBounds(165, 128, 168, 20);
		reportPanel.add(comboBox_rep_agent);
		
		JButton viewSumaryBtn = new JButton("View Details");
		viewSumaryBtn.setEnabled(false);
		viewSumaryBtn.setVisible(false);
		viewSumaryBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String fromDate = textFromDate.getText();
				String toDate ="";		
				
				if(textFromDate.getText().equals("")||textFromDate.getText().equals(null)){
					toDate = getDate();					
				}else{
					toDate = textToDate.getText();				
				}	
				
				
				if (fromDate.equals("")) {
					JOptionPane.showMessageDialog(null,
							"Please enter the Date..!");
				} else {
					ViewGenerator f = new ViewGenerator(fromDate,toDate);
					f.setLocation(340, 250);
					f.setVisible(true);
				}

			}
		});
		viewSumaryBtn.setBounds(165, 159, 152, 23);
		reportPanel.add(viewSumaryBtn);
		
		JButton btnReportSummary = new JButton("View Summary");
		btnReportSummary.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {				
				//ViewGenerator ff = new ViewGenerator().getTotalSummary(fromDate, toDate);
				
				if (!textFromDate.getText().toString().equals("")
					&& !textToDate.getText().toString().equals("")
					&& !comboBox_rep_agent.getSelectedItem().toString().equals("Choose Agent")) {
					
					Agent agent;	
					String fromDate = textFromDate.getText();
					String toDate = textToDate.getText();
					
					agent = (Agent) comboBox_rep_agent.getSelectedItem();
					ViewGenerator f = new ViewGenerator(fromDate,toDate,agent);
					f.setLocation(340, 250);
					f.setVisible(true);
					
				}
				
				
				else {
					JOptionPane.showMessageDialog(null,
							"Please Enter all values Before Save");
				}
				/*String fromDate = textFromDate.getText();
				String toDate ="";		
				
				if(textFromDate.getText().equals("")||textFromDate.getText().equals(null)){
					toDate = getDate();					
				}else{
					toDate = textToDate.getText();				
				}	
				
				
				if (fromDate.equals("")) {
					JOptionPane.showMessageDialog(null,
							"Please enter the Date..!");
				} else {
					ViewGenerator f = new ViewGenerator(fromDate,toDate,1);
					f.setLocation(340, 250);
					f.setVisible(true);
				}		*/	
				
				
			}
		});
		btnReportSummary.setBounds(165, 184, 152, 23);
		reportPanel.add(btnReportSummary);
		
	
		
		

		/* Check which tab is selected */
		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int tabId = tabbedPane.getSelectedIndex();
				System.out.println(tabId);

				switch (tabId) {

				case 0:
					//tableItems ,de_tableItems
					DefaultTableModel model = (DefaultTableModel)tableItems.getModel(); 
					int rows = model.getRowCount(); 
					for(int i = rows - 1; i >=0; i--)
					{
					   model.removeRow(i); 
					}
										
					ld_lorry_combo.removeAllItems();
					ld_lorry_combo.insertItemAt("Choose Lorry", 0);
					ld_lorry_combo.setSelectedIndex(0);

					ld_agent_combo.removeAllItems();
					ld_agent_combo.insertItemAt("Choose Agent", 0);
					ld_agent_combo.setSelectedIndex(0);

					ld_route_combo.removeAllItems();
					ld_route_combo.insertItemAt("Choose Route", 0);
					ld_route_combo.setSelectedIndex(0);

					ArrayList<Lorry> lorryList = Common.loadlorries();
					for (Lorry lorry : lorryList) {
						ld_lorry_combo.addItem(lorry);
					}

					ArrayList<Agent> agentList = Common.loadagents();
					for (Agent agent : agentList) {
						ld_agent_combo.addItem(agent);
					}

					ArrayList<Route> routeList = Common.loadroutes();
					for (Route route : routeList) {
						ld_route_combo.addItem(route);
					}
					break;

				case 1:
					
					// Day End tab
					
					DefaultTableModel model2 = (DefaultTableModel)de_tableItems.getModel(); 
					int rows2 = model2.getRowCount(); 
					for(int i = rows2 - 1; i >=0; i--)
					{
					   model2.removeRow(i); 
					}
					
					de_lorry_comboBox.removeAllItems();
					de_lorry_comboBox.insertItemAt("Choose lorry", 0);
					de_lorry_comboBox.setSelectedIndex(0);

					ArrayList<Lorry> de_lorryList = Common.loadlorries();
					for (Lorry lorry : de_lorryList) {
						de_lorry_comboBox.addItem(lorry);
					}

					break;
				case 2:

					// Add Element tab
					int id = 1;
					boolean check = PasswordChecker.checkResult(id);
					// Uses boolean to determine if password is valid
					if (check == true) {
						System.out.println("Correct password");

						if (addElementTabClickCount == 0) {

							Object[] listObject = addElements
									.loadDataAddElementsFrame();

							agents = (ArrayList) listObject[0];
							lorries = (ArrayList) listObject[1];
							routes = (ArrayList) listObject[2];

							System.out.println(", agency? " + agents.size());

							Iterator itr = agents.iterator();
							int count = 0;
							while (itr.hasNext()) {

								modelAgency.add(count, itr.next());
								count++;

							}

							itr = lorries.iterator();
							count = 0;
							while (itr.hasNext()) {

								modelLorry.add(count, itr.next());
								count++;

							}

							itr = routes.iterator();
							count = 0;
							while (itr.hasNext()) {

								modelRoute.add(count, itr.next());
								count++;

							}

						}

					} else {
						if (JOptionPane.showConfirmDialog(null,
								"Incorrect Password. Re-enter?",
								"wholesale.com", JOptionPane.OK_CANCEL_OPTION) == 0) {
							tabbedPane.setSelectedIndex(0);
							tabbedPane.setSelectedIndex(2);
						} else {
							tabbedPane.setSelectedIndex(0);
						}

					}

					break;


				case 3:
					// Stock Detail tab

					int id2 = 1;
					boolean check2 = PasswordChecker.checkResult(id2);

					if (check2 == false) {

						if (JOptionPane.showConfirmDialog(null,
								"Incorrect Password. Re-enter?",
								"wholesale.com", JOptionPane.OK_CANCEL_OPTION) == 0) {
							tabbedPane.setSelectedIndex(0);
							tabbedPane.setSelectedIndex(3);
							break;
						} else {
							tabbedPane.setSelectedIndex(0);
							break;
						}

					} else {

						sd_itemName_comboBox.removeAllItems();
						sd_itemName_comboBox.insertItemAt("Choose Item", 0);
						sd_itemName_comboBox.setSelectedIndex(0);

						ArrayList<Item> sd_itemList = Common.loadItems();
						for (Item item : sd_itemList) {
							items.add(item.toString());
							sd_itemName_comboBox.addItem(item);
						}
						
						
				
						Iterator itr = items.iterator();
						int count = 0;
						while (itr.hasNext()) {

							modelItem.add(count, itr.next());
							count++;
						}
						break;					
				
					}
					
				case 4:
					comboBox_rep_agent.removeAllItems();
					comboBox_rep_agent.insertItemAt("Choose Agent", 0);
					comboBox_rep_agent.setSelectedIndex(0);
					
					ArrayList<Agent> report_agentList = Common.loadagents();
					for (Agent agent : report_agentList) {
						comboBox_rep_agent.addItem(agent);
					}
					
				}
			}

		});

	}

	private void clearFields(int x) {

		if (x == 1)
			ci_agentName_textField.setText("");
		if (x == 2)
			ci_lorryName_textField.setText("");
		if (x == 3)
			ci_routeName_textField.setText("");
		if (x == 4)
			ci_itemName_textField.setText("");

	}
}
